﻿using CPPEI.ETL.DataServiceInterface;
using CPPEI.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ENT = CPPEI.ETL.Domain.Entities;
using MOD = CPPEI.ETL.Domain.Models;
using SER = CPPEI.ETL.Domain.Services;
using VIW = CPPEI.ETL.Domain.Views;

namespace CPPEI.ETL.WebApi.DataTable
{
    [CPPEI.WebApi.AuthGetAction("DataTable", "GetColumns", "1.0")]
    public class GetColumnsAction : BaseAction<GetColumnsParams, GetColumnsResult>
    {
        protected override ActionResult<GetColumnsResult> OnExecute(ActionContext context,
            SysParameters sysParameters,
            GetColumnsParams opParams)
        {
            SER.DtTableService service = new SER.DtTableService();

            SingleResult<CPPEI.ETL.Domain.Data.DataTableAndColumns> serResult = service.GetTableAndColumns(opParams.DataTableId);
            if (serResult.IsSuccess)
            {
                GetColumnsResult actResult = new GetColumnsResult();
                actResult.Id = serResult.Data.DataTable.Id;
                actResult.Name = serResult.Data.DataTable.Name;
                actResult.CreatedTime = serResult.Data.DataTable.CreatedTime;
                actResult.IsRawDt = serResult.Data.DataTable.IsRawDt;
                actResult.Columns = new List<GetColumnsResult.Column>();

                for (int rowIndex = 0; rowIndex < serResult.Data.Columns.Count; rowIndex++)
                {
                    GetColumnsResult.Column column = new GetColumnsResult.Column();

                    column.Id = serResult.Data.Columns[rowIndex].Id;
                    column.Name = serResult.Data.Columns[rowIndex].Name;

                    column.CreatedTime = serResult.Data.Columns[rowIndex].CreatedTime;

                    column.Nullable = serResult.Data.Columns[rowIndex].Nullable;

                    column.Length = serResult.Data.Columns[rowIndex].Length;

                    column.Type = serResult.Data.Columns[rowIndex].Type;
                    actResult.Columns.Add(column);
                }


                return new ActionResult<GetColumnsResult>(actResult);
            }
            else
            {
                return new ActionResult<GetColumnsResult>(serResult.IsSuccess, serResult.ErrorCode, serResult.Message, null);
            }
        }
    }

    public class GetColumnsParams
    {
        [Parameter]
        public Guid DataTableId { get; set; }
    }

    public class GetColumnsResult
    {
        public Guid Id { get; set; }
        public string Name { get; set; }


        public DateTime CreatedTime { get; set; }

        public bool IsRawDt { get; set; }

        public List<Column> Columns { get; set; }
        public class Column
        {
            public Guid Id { get; set; }
            public string Name { get; set; }

            public DateTime CreatedTime { get; set; }

            public bool Nullable { get; set; }

            public int Length { get; set; }

            public string Type { get; set; }
        }
    }
}
